package com.travel.Service.Impl;

import cn.hutool.json.JSONUtil;
import com.alipay.api.domain.AlipayCommerceEducateFacefeatureGroupkeyQueryModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.travel.Bean.Culture;
import com.travel.Bean.Manager;
import com.travel.Dao.CultureMapper;
import com.travel.JPA.CultureJPA;
import com.travel.JPA.ManagerJPA;
import com.travel.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class CultureServiceImpl {

    @Autowired
    private CultureJPA cultureJPA;
    @Autowired
    private CultureMapper cultureMapper;
    @Autowired
    private ManagerJPA managerJPA;
    @Autowired
    private RedisUtil redisUtil;


    private final String redisKey = "Travel_Culture:";
    /**
     * 选取景区列表
     *
     * @param cultureName 景区名字
     * @param page        页面
     * @param limit       记录条数
     * @return {@link PageInfo}<{@link Culture}>
     */
    @Cacheable(value = redisKey+"_List")
    public PageInfo<Culture> selCultureList(String cultureName,Integer page,Integer limit){

        PageHelper.startPage(page,limit);
        List<Culture> cultures = cultureMapper.selCultureList(cultureName);

        return new PageInfo<>(cultures);
    }

    /**
     * 找到景区通过id
     *
     * @param id id
     * @return {@link Culture}
     */

    @Cacheable(value = "Travel_Culture",key = "#id")
    public Culture findCultureById(Integer id){
        Culture culture = cultureJPA.findById(id).get();
        //如果景区简介为null则替换为""空
        if(culture.getSpot_summary() == null)
            culture.setSpot_summary("");
        return culture;
    }

    /**
     * 更新景区编辑
     *
     * @param culture 景区
     * @return {@link Integer}
     */
    @CacheEvict(value = "Travel_Culture",key = "#culture.id")
    public Integer updateCultureEdit(Culture culture){
//        Culture culture1 = cultureJPA.save(culture);
        if(cultureMapper.updateCultureEdit(culture)>0)
            return 0;
        return 1;
    }

    /**
     * 保存景区添加
     *
     * @param culture  景区
     * @param username 用户名
     * @return {@link Integer}
     */
    @Transactional
    public Integer saveCultureAdd(Culture culture,String username){


        Integer managerId = managerJPA.findIdByUsername(username);

        culture.setManager_id(managerId);

        Culture culture1 = cultureJPA.save(culture);

        if(culture1.getId() != null){
            if(managerJPA.updateManagerCid(culture1.getId(),username)>0)
                return 0;
            return 2;
        }
        //删除redis中的缓存
        redisUtil.delKeys(redisKey+"_List");
        return 1;
    }


    /**
     * 删除景区
     *
     * @param id id
     * @return {@link Integer}
     */
    @Transactional
    public Integer deleteCulture(Integer id){
        int res = cultureJPA.deleteByid(id);
        redisUtil.delKeys(redisKey+"_List");
        return res;
    }


    /**
     * 得到景区名字列表
     * id,spot_name,spot_opentime
     * @param spot_name 地方名字
     * @return {@link List}<{@link Culture}>
     */

    @Cacheable(value = redisKey+"_User_List")
    public List<Culture> getCultureNameList(String spot_name){
        List<Object[]> cultures;
        if(spot_name == null){
            cultures = cultureJPA.findUserCultureByName("");
        }else{
            cultures = cultureJPA.findUserCultureByName(spot_name);
        }

        //将查询到的list<map>格式转换成list<culture>类型
        List<Culture> cultureList = new ArrayList<>();
        for(Object[] object:cultures){
            Culture culture = new Culture();
            culture.setId(Integer.valueOf(object[0].toString()));
            culture.setSpot_name(object[1].toString());
            culture.setSpot_opentime(object[2].toString());
            cultureList.add(culture);
        }

        return cultureList;
    }




}
